<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/

if (!defined ('EXPLAY')) exit;

class imageResize {

	private $old_src;
	private $new_dir;
	private $new_name;
	private $type;
	private $save_type; // при отключенном save_type изоображение будет сохраняться в формате JPG
	private $max_size; // arrray (width, height)
	private $jpg_compress;
	private $new_image_src = false;
	
	public function __construct ($old_src, $new_name, $new_dir = '', $max_size = array (), $save_type = false, $jpg_compress = 70) {
		
		if ($old_src == '' || $new_name == '' || !is_array ($max_size)) throw new Exception ('Ошибка данных!');
		
		$this->old_src = $old_src;
		$this->new_dir = $new_dir;
		$this->new_name = $new_name;
		$this->max_size = $max_size;
		$this->save_type = $save_type;
		$this->jpg_compress = $jpg_compress;
		
	}
	
	public function resize ($delete_old = true) {
	
		$src = $this->old_src;
		
		if (!file_exists($src)) throw new Exception ('Не существует файла!');
		
		if(eregi('[A-Za-z0-9_]\.(jpeg|jpg|JPEG|JPG)', $this->old_src)) {
			$thumbbase = imagecreatefromjpeg($src);
			$type = 'jpg';
		}
		elseif(eregi('[A-Za-z0-9_]\.(gif|GIF)', $this->old_src)) {
			$thumbbase = imagecreatefromgif($src);
			$type = 'gif';
		}
		elseif(eregi('[A-Za-z0-9_]\.(png|PNG)', $src)) {
			$thumbbase = imagecreatefrompng($src);
			$type = 'png';
		}
		elseif(eregi('[A-Za-z0-9_]\.(bmp|wbmp|BMP|wbmp)', $src)) {
			$thumbbase = imagecreatefromwbmp($src);
			$type = 'bmp';
		}
		else
			throw new Exception ('Файл не является изоображенем!');
		
		$width = imagesx($thumbbase);
		$height = imagesy($thumbbase);
		
		if (!isset ($this->new_size[0]) || !isset ($this->new_size[1]))
			$this->new_size = array ($width, $height);
		
		if ($width > $this->max_size[0] || $height > $this->max_size[1]) {
			$k = max($width/$this->max_size[0], $height/$this->max_size[1]);
			$new_width = intval($width / $k);
			$new_height = intval($height / $k);
		}
		else {
			$new_width = $width;
			$new_height = $height;
		}
		
		$thumb = imagecreatetruecolor ($new_width, $new_height);
		imagecopyresampled($thumb, $thumbbase, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
		
		$name = ($this->save_type) ? $this->new_dir.'/'.$this->new_name.'.'.$type : $this->new_dir.'/'.$this->new_name.'.jpg';
		
		if ($this->save_type == false) {
			
			if (imagejpeg($thumb, $_SERVER['DOCUMENT_ROOT'].'/'.$name, $this->jpg_compress)) {
				$this->new_image_src = $name;
				imagedestroy($thumb);
				imagedestroy($thumbbase);
				if ($delete_old) @unlink ($src);
				return true;
			}
			else return false;
		}
		
		switch ($type) {
		
			case 'jpg' : {
				if (imagejpeg($thumb, $_SERVER['DOCUMENT_ROOT'].'/'.$name, $this->jpg_compress)) {
					$this->new_image_src = $name;
					imagedestroy($thumb);
					imagedestroy($thumbbase);
					if ($delete_old) @unlink ($src);
					return true;
				}
				else return false;
			}
			
			case 'gif' : {
				if (imagegif ($thumb, $_SERVER['DOCUMENT_ROOT'].'/'.$name)) {
					$this->new_image_src = $name;
					imagedestroy($thumb);
					imagedestroy($thumbbase);
					if ($delete_old) @unlink ($src);
					return true;
				}
				else return false;
			}
			
			case 'png' : {
				if (imagepng ($thumb, $_SERVER['DOCUMENT_ROOT'].'/'.$name)) {
					$this->new_image_src = $name;
					imagedestroy($thumb);
					imagedestroy($thumbbase);
					if ($delete_old) @unlink ($src);
					return true;
				}
				else return false;
			}
			
			case 'bmp' : {
				if (imagewbmp ($thumb, $_SERVER['DOCUMENT_ROOT'].'/'.$name)) {
					$this->new_image_src = $name;
					imagedestroy($thumb);
					imagedestroy($thumbbase);
					if ($delete_old) @unlink ($src);
					return true;
				}
				else return false;
			}
			
			default : return false;
		
		}
		
		throw new Exception ('Невозможное случилось... голактико опастносте!');
		
	
	}
	
	public function __toString () {
		return (string) $this->new_image_src;
	}
	
}